********************************************************************************************************
*                        RUN REGRESSIONS WITH HETEROGENEITY BY HOSPITAL CHARACTERISTIC                 *
* Last updated: 7/20/2021																			   *
********************************************************************************************************
log using "${SIDCodePath}/sidsedd_regressions_trad_hetero.log", replace
use "${SIDDataPath}/sidsedd_merged_20102016.dta", clear
destring dshospid, gen(dshospid3)
preserve
	use "${SIDDataPath}/HCUP-server-data/florida_hosp_chars.dta", clear // available in replication packet
	rename dshospid dshospid3
	tempfile temp
	save `temp'
restore

merge m:1 dshospid3 using `temp', keep(1 3)

//---------------------------------------------------------------------//
// HETEROGENEITY REGRESSIONS WITH PATIENT CHARACTERISTIC FIXED EFFECTS //
//--------------------------------------------------------------------//

	*--------------------------------------------------
	* Difference-in-RD 
	*--------------------------------------------------
	foreach var of varlist		inpatient {
		forval t = 3/3 {
			preserve
				keep	if trad_medicare == 1
				keep 	if hourstilMN`t' == 1

				eststo clear 

				foreach heterovar of varlist urban teaching hospprofittype independent_2011 above_avg_beds app_contracted_136{
					reghdfe `var' post_qtr_after_MN##i.`heterovar', ///
						absorb(edhour_2 year_dqtr dshospid agebin##female race2 hispanic2 pointoforigin2 sincevi_30 nchronic i.edhour_2##i.dshospid2 i.year_dqtr##i.dshospid2 medincstq) /// 
							vce(cluster edhour_2 year_dqtr)
					eststo 	`heterovar', ///
							title(proper("`var'"))

					estadd   local hospitaltimeFE "X"
					estadd   local patientFE 	  "X"
					estadd 	 local MNsample 	  "`t'"
					estadd 	 local pop 			  "IP & OP"
					estadd   local medsample 	  "All"

					regsave using "${SIDOutputPath}/reg_trad_patientFEs/reg_med_hosphetero_3h_`heterovar'_diRD.dta", replace
				}
				esttab ///
					using "${SIDOutputPath}/reg_trad_patientFEs/reg_med_ip_hosphetero_3h_diRD.tex", replace /// 
					star(* .10 ** .05 *** .01) ///
					scalar("hospitaltimeFE Hospital-time FE" "patientFE Patient FE" "MNsample Midnight hours range" "pop Sample" "medsample Sample" "N Observations") /// 
					coeflabels(1.post_qtr_after_MN "$ Post_t \times AfterMN_h $" 1.post_qtr_after_MN#1.urban "$ \times Urban $" ///
						1.post_qtr_after_MN#1.teaching "$ \times Teaching $ " 1.post_qtr_after_MN#2.hospprofittype "$ \times For-profit $"  ///
						1.post_qtr_after_MN#3.hospprofittype "$ \times Gov't $ " 1.post_qtr_after_MN#1.independent "$ \times Ind. $ " 1.post_qtr_after_MN#1.above_avg_beds "$ \times Above Avg Beds $ " ///
						1.post_qtr_after_MN#1.app_contracted_136 "$ \times Med. Necc. App $ ") /// 
					b(%10.3f) noobs	sfmt(0)	nodepvars numbers se noconstant ///
					keep(1.post_qtr_after_MN 1.post_qtr_after_MN#*) ///
					addnotes("Hospital-time FE: hospital, hospital-quarter, and hospital-hour" "Patient FE: age-sex bin, race, hispanic, point of origin, last ED visit 30 days, n. chronic conditions, zip code income" )


			restore

		}
	} 


	

log close